using System;
using System.Diagnostics;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using ComLib.ComFun;
using ComLib.ComType;
using System.IO;

namespace YSLPages
{
	public class FingerprintManage
	{
        public delegate void CaptureStatusCallBack(int code, string msg);
        public event CaptureStatusCallBack captureCallBack;

        IntPtr _hwndParent = new IntPtr();
        IntPtr _hwndEidtYwbh = new IntPtr();
        IntPtr _hwndAddYwbh = new IntPtr();
        IntPtr _hwndTipsLabel = new IntPtr();

        bool bExitCapture = false;
		public IntPtr OpenFingerprint()
		{
			this.KillFingerprint();
            //ProcessStartInfo info = new ProcessStartInfo(@"C:\Users\jin\Documents\Tencent Files\362018819\FileRecv\ViewWizard2.exe");
            Directory.SetCurrentDirectory(Path.Combine(Application.StartupPath, "FingerCaptureSystem"));
            Process.Start("FingerPrintCaptureSystem.exe");
            //Process.Start(info);
            IntPtr intPtr = IntPtr.Zero;
			for (int i = 0; i < 100; i++)
			{
                intPtr = WinAPI.FindWindowEx(IntPtr.Zero, IntPtr.Zero, "#32770", "出入境管理生物特征采集系统");
				if (intPtr != IntPtr.Zero)
				{
					break;
				}
				Thread.Sleep(100);
			}            

			if (intPtr != IntPtr.Zero)
			{
                IntPtr hwad = WinAPI.FindWindowEx(intPtr, IntPtr.Zero, "Edit", null);
                WindowsAPI.SendMessage(hwad, 12, 0, "123321123"); //输入
                IntPtr hwad2 = WinAPI.FindWindowEx(intPtr, IntPtr.Zero, null, "确定");
                WindowsAPI.SendMessage(hwad2, 245, 0, "");   //点击确定按钮
				//IntPtr value = IntPtr.Zero;
                bool isInitOK = false;
				for (int i = 0; i < 150; i++)
				{
                    intPtr = WinAPI.FindWindowEx(IntPtr.Zero, IntPtr.Zero, "#32770", "出入境管理生物特征采集系统");
                    if (intPtr != IntPtr.Zero)
                    {
                        IntPtr intPtr1 = WinAPI.FindWindowEx(IntPtr.Zero, IntPtr.Zero, "#32770", "错误");
                        IntPtr intPtr2 = WinAPI.FindWindowEx(IntPtr.Zero, IntPtr.Zero, "#32770", "提示");
                        if (intPtr1 != IntPtr.Zero || intPtr2 != IntPtr.Zero)
                        {
                            return IntPtr.Zero;
                        }
                        _hwndParent = intPtr;
                        _hwndTipsLabel = WinAPI.FindWindowEx(_hwndParent, IntPtr.Zero, "Static", "系统初始化完毕!");
                        if (_hwndTipsLabel == IntPtr.Zero)
                        {
                            Thread.Sleep(100);
                            continue;  
                        }                 
                        isInitOK = true;
                        _hwndEidtYwbh = WinAPI.FindWindowEx(intPtr, IntPtr.Zero, null, "受理号");                        
                        if (_hwndEidtYwbh != IntPtr.Zero)
                        {

                            _hwndAddYwbh = WinAPI.FindWindowEx(intPtr, IntPtr.Zero, null, "添加条码&A");
                            break;
                        }
                    }
                    
					Thread.Sleep(100);
				}
                Directory.SetCurrentDirectory(Application.StartupPath);
                if (!isInitOK)
                {
                    //MessageBox.Show("指纹仪初始化失败");
                    return IntPtr.Zero;
                }
                if (_hwndEidtYwbh != IntPtr.Zero)
                {
                    //Common.EnumWindowsProc lpEnumFunc = new Common.EnumWindowsProc(this.EnumWindows);
                    //Common.EnumChildWindows(intPtr, lpEnumFunc, 1);
                }
                else
                {
                    //Common.cwText += "嵌套指纹仪界面失败 ";
                    this.KillFingerprint();
                }
                IntPtr value2 = WinAPI.FindWindowEx(IntPtr.Zero, IntPtr.Zero, null, "提示");
				if (value2 != IntPtr.Zero)
				{
					//Common.cwText += "加载指纹采集设备失败 ";
					this.KillFingerprint();
				}
			}
			else
			{
				//Common.cwText += "打开指纹仪软件失败 ";
			}
            Directory.SetCurrentDirectory(Application.StartupPath);
			return intPtr;
		}
        //private bool EnumWindows(IntPtr hWnd, int lParam)
        //{
        //    StringBuilder stringBuilder = new StringBuilder(255);
        //    Common.GetClassName(hWnd, stringBuilder, stringBuilder.MaxCapacity);
        //    if (stringBuilder.ToString() == "Button")
        //    {
        //        Common.ShowWindow(hWnd, 0);
        //    }
        //    return true;
        //}
        public bool StartFingerprintGather(IntPtr hand, string ywbh, CaptureStatusCallBack capCallBack)
		{
			bool result;
            captureCallBack += capCallBack;
			if (hand == IntPtr.Zero)
			{
				result = false;
			}
			else
			{
                IntPtr hwad = WinAPI.FindWindowEx(hand, IntPtr.Zero, "Edit", null);
                WindowsAPI.SendMessage(hwad, 12, 0, ywbh);
                WindowsAPI.PostMessage(hwad, 256, 13, 0);
				Thread.Sleep(500);
                hwad = WinAPI.FindWindowEx(hand, IntPtr.Zero, null, "左手&L");
                WindowsAPI.SendMessage(hwad, 245, 0, "");
                hwad = WinAPI.FindWindowEx(hand, IntPtr.Zero, null, "右手&R");
                WindowsAPI.SendMessage(hwad, 245, 0, "");
                hwad = WinAPI.FindWindowEx(hand, IntPtr.Zero, null, "&6");
                WindowsAPI.SendMessage(hwad, 245, 0, "");
                hwad = WinAPI.FindWindowEx(hand, IntPtr.Zero, null, "&1");
                WindowsAPI.SendMessage(hwad, 245, 0, "");
                hwad = WinAPI.FindWindowEx(hand, IntPtr.Zero, null, "开始采集&C");
                WindowsAPI.SendMessage(hwad, 245, 0, "");
				Thread.Sleep(500);

                bExitCapture = false;
                //开启监控
                Thread thread = new Thread(CaptureMonitor);
                thread.IsBackground = true;
                thread.Start();
                result = true;
			}
			return result;
		}
		public bool StopFingerprintGather(IntPtr hand)
		{
			bool result;
            bExitCapture = true;
			if (hand == IntPtr.Zero)
			{
				result = false;
			}
			else
			{
                hand = WinAPI.FindWindowEx(hand, IntPtr.Zero, null, "指纹重采&E");
                WindowsAPI.SendMessage(hand, 245, 0, "");
				result = true;
			}
			return result;
		}

		public Process[] GetFingerprintProcess()
		{
			Process[] processesByName = Process.GetProcessesByName("FingerPrintCaptureSystem");
			if (processesByName.Length == 0)
			{
				processesByName = Process.GetProcessesByName("出入境管理生物特征采集系统");
			}
			if (processesByName.Length == 0)
			{
				processesByName = Process.GetProcessesByName("出入境管理生物特征采集系统-测试版");
			}
			return processesByName;
		}


		public void KillFingerprint()
		{
            bExitCapture = true;
            captureCallBack = null;

			Process[] fingerprintProcess = this.GetFingerprintProcess();
			if (fingerprintProcess.Length > 0)
			{
				fingerprintProcess[0].Kill();
			}
		}

        public bool InputYwbh()
        {
            IntPtr hwndOkButton = new IntPtr();
            if (_hwndEidtYwbh != IntPtr.Zero)
            {
                WindowsAPI.SendMessage(_hwndEidtYwbh, 12, 0, "123456789012345");
                Thread.Sleep(500);
                if (_hwndAddYwbh != IntPtr.Zero)
                {
                    WindowsAPI.SendMessage(_hwndAddYwbh, 245, 0, "");
                    for (int i = 0; i < 10; i++)
                    {
                        Thread.Sleep(500);
                        hwndOkButton = WinAPI.FindWindowEx(_hwndParent, IntPtr.Zero, null, "OK");
                        if (hwndOkButton != IntPtr.Zero)
                        {
                            WindowsAPI.SendMessage(hwndOkButton, 245, 0, "");
                            return true;
                        }
                    }
                }
                
            }

            return false;
        }

        public string CaptureStatus()
        {
            if (IntPtr.Zero == _hwndTipsLabel)
            {
                return "";
            }

            int length = WinAPI.GetWindowTextLength(_hwndTipsLabel);
            StringBuilder windowName = new StringBuilder(length + 1);
            WinAPI.GetWindowText(_hwndTipsLabel, windowName, windowName.Capacity);
            return windowName.ToString();
        }

        private void CaptureMonitor()
        {
            while (!bExitCapture && captureCallBack != null)
            {
                try
                {
                    string status = CaptureStatus();
                    if (status.Contains("数据上传完毕"))
                    {
                        captureCallBack(0, status);
                        this.KillFingerprint();
                        break;
                    }
                    else if (status.Contains("异常"))
                    {
                        captureCallBack(-1, status);
                        break;
                    }
                    else if (status.Contains("现在正在采集右手拇指"))
                    {
                        captureCallBack(1, status);
                        //break;
                    }
                    else if (status.Contains("现在正在采集右手食指"))
                    {
                        captureCallBack(2, status);
                        //break;
                    }
                    else if (status.Contains("现在正在采集右手中指"))
                    {
                        captureCallBack(3, status);
                        //break;
                    }
                    else if (status.Contains("现在正在采集右手环指"))
                    {
                        captureCallBack(4, status);
                        //break;
                    }
                    else if (status.Contains("现在正在采集右手小指"))
                    {
                        captureCallBack(5, status);
                        //break;
                    }
                    else if (status.Contains("现在正在采集左手拇指"))
                    {
                        captureCallBack(6, status);
                        //break;
                    }
                    else if (status.Contains("现在正在采集左手食指"))
                    {
                        captureCallBack(7, status);
                        //break;
                    }
                    else if (status.Contains("现在正在采集左手中指"))
                    {
                        captureCallBack(8, status);
                        //break;
                    }
                    else if (status.Contains("现在正在采集左手环指"))
                    {
                        captureCallBack(9, status);
                        //break;
                    }
                    else if (status.Contains("现在正在采集左手小指"))
                    {
                        captureCallBack(10, status);
                        //break;
                    }
                    Thread.Sleep(200);
                }
                catch
                {
                }
            }
        }
	}
}
